package cn.waka.sys.admin.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.concurrent.TimeUnit;


@Service
public class RedisService {
    StringRedisTemplate template;

    @Autowired
    public  RedisService(RedisConnectionFactory factory) {
        template = new StringRedisTemplate(factory);
    }

    //public void setPool(JedisPool pool) {
     //   this.pool = pool;
    //}

    public void incr(String key) {
        template.opsForValue().increment(key,1l);
        /*Jedis r = pool.getResource();
        try {
            r.incr(key);
        } finally {
            r.close();
        }*/
    }

    public void decr(String key) {
        template.opsForValue().increment(key,-1l);
        /*Jedis r = pool.getResource();
        try {
            r.decr(key);
        } finally {
            r.close();
        }*/
    }

    public void incrby(String key, int num) {
        template.opsForValue().increment(key,num);
        /*Jedis r = pool.getResource();
        try {
            r.incrBy(key, num);
        } finally {
            r.close();
        }*/
    }

    public void decrby(String key, int num) {
        template.opsForValue().increment(key,num);
        /*Jedis r = pool.getResource();
        try {
            r.decrBy(key, num);
        } finally {
            r.close();
        }*/
    }


    public String get(String key) {

        /*Jedis r = pool.getResource();
        try {
            return r.get(key);
        } finally {
            r.close();
        }*/
        return template.opsForValue().get(key);

    }

    public void expire(String key, int time) {
        /*Jedis r = pool.getResource();
        try {
            r.expire(key, time);
        } finally {
            r.close();
        }*/
        template.expire(key,time, TimeUnit.SECONDS);

    }

    public void set(String key, String value) {
        /*Jedis r = pool.getResource();
        try {
            r.set(key, value);
        } finally {
            r.close();
        }*/
        template.opsForValue().set(key,value);
    }

    public boolean exists(String key) {
        /*Jedis r = pool.getResource();
        try {
            return r.exists(key);
        } finally {
            r.close();
        }*/
        return template.hasKey(key);
    }

    public boolean setnx(String key, String value) {
        /*Jedis r = pool.getResource();
        try {
            return r.setnx(key, value) > 0;
        } finally {
            r.close();
        }*/

        return template.opsForValue().setIfAbsent(key,value);
    }

    public void publish(String key, String value) {
       /* Jedis r = pool.getResource();
        try {
            r.publish(key, value);
        } finally {
            r.close();
        }*/

    }

    public void del(String key) {
        /*Jedis r = pool.getResource();
        try {
            r.del(key);
        } finally {
            r.close();
        }*/
        template.delete(key) ;
    }

    public void dels(final String[] keys) {
        /*Jedis r = pool.getResource();
        try {
            r.del(keys);
        } finally {
            r.close();
        }*/
        List<String> list=new ArrayList<String>();
        for(String k:keys){
            list.add(k);
        }
        template.delete(list);
    }

    public Set<String> keys(String pre) {
       /* Jedis r = pool.getResource();
        try {
            return r.keys(pre + "*");
        } catch (Exception ex) {
            return null;
        } finally {
            r.close();
        }*/
       return template.opsForSet().members(pre+"*");
    }
}
